package com.qyhl.module_practice.donate.detail;

import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.LoadingDialog;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.AndroidBug5497Workaround;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PRACTICE_DONATE_DETAIL)
public class PracticeDonateDetailActivity extends BaseActivity implements PracticeDonateDetailContract.PracticeDonateDetailView {
    @BindView(R2.id.name)
    EditText name;
    @BindView(R2.id.address)
    EditText address;
    @BindView(R2.id.post_num)
    EditText postNum;
    @BindView(R2.id.phone)
    EditText phone;
    @BindView(R2.id.content)
    EditText content;
    @BindView(R2.id.remark)
    EditText remark;


    private LoadingDialog.Builder builder;

    private int addressId;

    private PracticeDonateDetailPresenter mPresenter;

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_donate_detail;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new PracticeDonateDetailPresenter(this);
        AndroidBug5497Workaround.assistActivity(findViewById(android.R.id.content));
        addressId = getIntent().getIntExtra("id", 0);

        builder = new LoadingDialog.Builder(PracticeDonateDetailActivity.this);
        builder.setTitle("提交中...");
        builder.setCanceledOnTouchOutside(false);
        builder.setCancelable(true);
    }

    @Override
    protected void setListener() {

    }

    @Override
    public void donateSuccess(String msg) {
        builder.dismiss();
        showToast(msg);
        finish();
    }

    @Override
    public void donateError(String msg) {
        builder.dismiss();
        showToast(msg);
    }

    private void checkData() {
        builder.show();

        if (!CommonUtils.getInstance().getUserCheck().equals("1") && CommonUtils.getInstance().getSite() == 217) {
            showToast("用户信息审核中，暂时无法使用该功能！", INFO);
            builder.dismiss();
            return;
        }

        String upName = name.getText().toString();

        String upAddress = address.getText().toString();

        String upPost = postNum.getText().toString();

        String upRemark = remark.getText().toString();

        String upPhone = phone.getText().toString();
        if (StringUtils.isEmpty(upPhone)) {
            showToast("联系方式不能为空！");
            builder.dismiss();
            return;
        }

        String upContent = content.getText().toString();
        if (StringUtils.isEmpty(upContent)) {
            showToast("捐赠内容不能为空！");
            builder.dismiss();
            return;
        }

        mPresenter.doDonate(addressId + "", upName, upAddress, upPhone, upPost, upContent, upRemark);

    }

    @OnClick({R2.id.back_btn, R2.id.commit_btn})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.back_btn) {
            finish();
        } else if (id == R.id.commit_btn) {
            checkData();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_DONATE_DETAIL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_DONATE_DETAIL);
    }
}
